package cn.powertime.evaluation.service.impl;

import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.CpsTestPaper;
import cn.powertime.evaluation.mapper.CpsTestPaperMapper;
import cn.powertime.evaluation.service.CpsTestPaperService;
import cn.powertime.evaluation.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 试卷表 服务实现类
 * </p>
 *
 * @author Yang Xin
 * @since 2019-09-25
 */
@Service
public class CpsTestPaperServiceImpl extends ServiceImpl<CpsTestPaperMapper, CpsTestPaper> implements CpsTestPaperService {

    @Autowired
    private CpsTestPaperMapper cpsTestPaperMapper;

    @Override
    public Page<RespTestPaperPageVo> courseWorkTestPaperList(Page page, ReqCourseTestPaperSearchVo vo) {
        return this.cpsTestPaperMapper.selectcourseWorkTestPaperListPage(page, vo);
    }

    @Override
    public CpsTestPaper selectTestPaperBySectionId(Long sectionId) {
        QueryWrapper<CpsTestPaper> qw = new QueryWrapper<>();
        qw.eq("status", Constants.STATUS_NORMAL);
        qw.eq("test_type", 1);//1:课后作业
        qw.eq("section_id", sectionId);
        qw.orderByDesc("id");
        List<CpsTestPaper> list = this.list(qw);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public CpsTestPaper selectTestPaperByCourseId(Long courseId) {
        QueryWrapper<CpsTestPaper> qw = new QueryWrapper<>();
        qw.eq("status", Constants.STATUS_NORMAL);
        qw.eq("test_type", 2);//2:课程刷题
        qw.eq("course_id", courseId);
        qw.orderByDesc("id");
        List<CpsTestPaper> list = this.list(qw);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<RespTopicListVo> selectTopicListByTestPaperId(Long userId, Long testPaperId) {
        return this.cpsTestPaperMapper.selectTopicListByTestPaperId(userId, testPaperId);
    }

    @Override
    public Page<RespTestPaperPageVo> simulateExaminationPaperList(Page page) {
        return this.cpsTestPaperMapper.selectSimulateExaminationPaperPage(page);
    }

    @Override
    public Page<RespTestPaperPageVo> testing(Page<CpsTestPaper> page, ReqTestPaperSearchVo vo) {
        return this.cpsTestPaperMapper.selectTestingPage(page, vo);
    }

    @Override
    public Page<RespAnswerTestPaperVo> answerRecord(Page page, ReqAnswerTestPaperSearchVo vo, Long userId) {
        return this.cpsTestPaperMapper.selectAnswerRecordPage(page, vo, userId);
    }

    @Override
    public List<RespAnswerTopicListVo> testPaperView(Long userId, Long testPaperId, Long resultId) {
        return this.cpsTestPaperMapper.selectTestPaperView(userId, testPaperId, resultId);
    }

    @Override
    public List<RespTopicListVo> appSelectTopicListByTestPaperId(Long testPaperId) {
        return  this.cpsTestPaperMapper.appSelectTopicListByTestPaperId(testPaperId);
    }

}
